package org.shock.ss.sms.commons.db;

final public class Page {
	private int pagecount = 0;

	private int pagenum;

	private int nowpage;

	private int rows;

	private int rownum;

	private int pageno;

	private String pageString="";

	private String errors = "";

	public Page() {
	}

	public void setPageString(int rows, int rownum, int pageno, int page__,
			String urlval) {
		this.rownum=rownum;
		this.pageno=pageno;
		this.rows=rows;
		if(rows%rownum==0){
			setPagecount(rows/rownum);
		}
		else{
			setPagecount(rows/rownum+1);
		}
		if(pageno<=0){
			pageno=1;
		}
		else if(pageno>pagecount){
			pageno=pagecount;
		}
		if(pageno%page__==0){
			nowpage=pageno/page__;
		}
		else{
			nowpage=pageno/page__+1;
		}
		if(pagecount%page__==0){
			pagenum=pagecount/page__;
		}
		else{
			pagenum=pagecount/page__+1;
		}
		if(pagenum==1){
			pageString="<FONT face='webdings'>9</FONT>&nbsp;";
			int i=1;
			while(i<=pagecount){
				if(i==pageno){
					pageString=pageString+"<a style='color:red'>"+i+"</a>&nbsp;";	
				}
				else{
					pageString=pageString+"<a href='?pageno="+i+"&"+urlval+"'>"+i+"</a>&nbsp;";
				}
				i++;
			}
			pageString=pageString+"<FONT face='webdings'>:</FONT>";
		}
		else if(pagenum>1){
			if(nowpage==1){
				pageString="<FONT face='webdings'>9</FONT><FONT face='webdings'>7</FONT>&nbsp;";
				int i=(nowpage-1)*page__+1;
				while(i<=nowpage*page__){
					if(i==pageno){
						pageString=pageString+"<a style='color:red'>"+i+"</a>&nbsp;";	
					}
					else{
						pageString=pageString+"<a href='?pageno="+i+"&"+urlval+"'>"+i+"</a>&nbsp;";
					}
					i++;
				}
				pageString=pageString+"<a href='?pageno=11&"+urlval+"' title='下十页'><FONT face='webdings'>8</FONT></a><a href='?pageno="+pagecount+"&"+urlval+"' title='尾页'><FONT face='webdings'>:</FONT></a>";
			}
			else if(nowpage>1 && nowpage<pagenum){
				pageString="<a href='?pageno=1&"+urlval+"' title='首页'><FONT face='webdings'>9</FONT></a><a href='?pageno="+((nowpage-1)*page__)+"&"+urlval+"' title='上十页'><FONT face='webdings'>7</FONT></a>&nbsp;";
				int i=(nowpage-1)*page__+1;
				while(i<=nowpage*page__){
					if(i==pageno){
						pageString=pageString+"<a style='color:red'>"+i+"</a>&nbsp;";	
					}
					else{
						pageString=pageString+"<a href='?pageno="+i+"&"+urlval+"'>"+i+"</a>&nbsp;";
					}
					i++;
				}
				pageString=pageString+"<a href='?pageno="+(nowpage*page__+1)+"&"+urlval+"' title='下十页'><FONT face='webdings'>8</FONT></a><a href='?pageno="+pagecount+"&"+urlval+"' title='尾页'><FONT face='webdings'>:</FONT></a>";
			}
			else if(nowpage>=pagenum){
				pageString="<a href='?pageno=1&"+urlval+"' title='首页'><FONT face='webdings'>9</FONT></a><a href='?pageno="+((nowpage-1)*page__)+"&"+urlval+"' title='上十页'><FONT face='webdings'>7</FONT></a>&nbsp;";
				int i=(nowpage-1)*page__+1;
				while(i<=pagecount){
					if(i==pageno){
						pageString=pageString+"<a style='color:red'>"+i+"</a>&nbsp;";	
					}
					else{
						pageString=pageString+"<a href='?pageno="+i+"&"+urlval+"'>"+i+"</a>&nbsp;";
					}
					i++;
				}
				pageString=pageString+"<FONT face='webdings'>8</FONT><FONT face='webdings'>:</FONT>";
			}
		}
		pageString = "总记录数:" + rows +"条&nbsp;&nbsp;"+ pageString;
	}

	public String getPageString() {
		return pageString;
	}

	public void setPagecount(int pagecount) {
		this.pagecount = pagecount;
	}

	public int getPagecount() {
		return pagecount;
	}

	public int getRows() {
		return rows;
	}

	public String getPageSql(String sql) {// pagenoΪ��ҳ
		String newsql = "select * from (select rownum r_id,t.*  from (" + sql
				+ ") t where rownum <= " + (pageno * rownum) + ") where r_id >"
				+ ((pageno - 1) * rownum);
		return newsql;
	}

	public void setErrors(String error) {
		this.errors = error;
	}

	public String getErrors() {
		return errors;
	}
}